#include"process.h"
#include<string.h>
#include<unistd.h>

#define LEN 101
#define LABLE '#'

void FlushProcess(double target, double current)
{
    const char* str = "|/一\\";
    int len = strlen(str);
    static int index = 0;

    double rate = (current / target) * 100.0;
    char bar[LEN];
    memset(bar, 0, sizeof(bar));
    int count = (int)rate;
    for(int i = 0; i < count; i++)
    {
        bar[i] = LABLE;
    }
    printf("[%-100s][%.1lf%%][%c]\r", bar, rate, str[index++]);
    index %= len;
    fflush(stdout);
    if(rate >= 100.0)
    {
        printf("\n");
    }
}

void Process()
{
    const char* str = "|/一\\";
    int str_len = strlen(str);
    char bar[LEN];
    memset(bar, '\0', sizeof(bar));
    int count = 0;
    while(count <= 100)
    {
        printf("[%-100s][%d%%][%c]\r", bar, count, str[count % str_len]);
        fflush(stdout);
        bar[count++] = LABLE;
        usleep(50000);
    }

    printf("\n");
}
